package com.store.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.store.store.entity.Employee;
import com.store.store.service.EmployeeService;
import com.store.store.util.R;
import lombok.extern.slf4j.Slf4j;
import org.mortbay.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        /**
         * 1.首先进行密码的md5加密
         * 2.判断用户名是否存在
         * 3.判断密码是否正确
         * 4.判断员工状态是否正常*/
        //1
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //2
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp= employeeService.getOne(lambdaQueryWrapper);
        if(emp==null) return R.error("用户名不存在");

        //3
        if(!emp.getPassword().equals(password)) return R.error("密码错误");

        //4
        if(emp.getStatus().equals("0")) return R.error("当前用户已锁定");

        //5登录成功，将用户id存入session，返回登录成功结果
        request.getSession().setAttribute("Employee",emp.getIdNumber());

        return R.success(emp);
    }

    /**
     * 员工登出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.清理Session中保存的当前员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(Employee employee){
        log.info("新增员工，员工信息{}",employee.toString());
        //设置初始密码，进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        employeeService.save(employee);
        return null;
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name){

        //1.构造分页构造器,page对象
        Page pageInfo = new Page(page,pageSize);

        //2.构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQuery = new LambdaQueryWrapper();
        lambdaQuery.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //添加排序条件
        lambdaQuery.orderByDesc(Employee::getUpdateTime);

        //3.执行查询,查询结果将封装到pageinfo中
        employeeService.page(pageInfo,lambdaQuery);
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        log.info(employee.toString());

        employeeService.updateById(employee);

        return null;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null)
            return R.success(employee);
        return R.error("此用户不存在");
    }
}
